package com.example.ch2_4noti;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;

public class MainActivity extends AppCompatActivity {

    private Button btnNotify;

    private Button btnContextMenu;


    private Button btnPopup;

    private PopupMenu popupMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取布局文件上的控件对象
        getViews();

        //绑定监听器
        setListeners();

        //给某个UI控件注册上下文菜单
        registerForContextMenu(btnContextMenu);



        //1.创建popupmenu对象
        popupMenu = new PopupMenu(this,//上下午环境
                btnPopup);//关联的UI控件
        //2.加载菜单资源并关联给popupmenu的菜单
        popupMenu.getMenuInflater().inflate(
                R.menu.file_menu,//菜单资源
                popupMenu.getMenu() //关联的弹出式菜单
        );
        //3.注册菜单项点击事件监听器
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                //判断当前选择的是哪一个菜单项
                switch (item.getItemId()){
                    case R.id.menu_file:
                        Log.i("msg","文件菜单");
                        break;
                    case R.id.menu_file_open:
                        Log.i("msg","文件-打开菜单");
                        break;
                    case R.id.menu_file_saves:
                        Log.i("msg","文件-另存为菜单");
                        break;
                    case R.id.menu_edit:
                        Log.i("msg","编辑菜单");
                        break;
                }
                return false;
            }
        });


        //4.显示弹出式菜单（通常在特定事件下触发，比如UI控件的点击事件）
        //在点击事件监听器下进行
//        popupMenu.show();  //写到这里程序直接挂掉



    }

    private void setListeners() {
        btnNotify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发布通知
                showNotification();
            }
        });

        //给按钮设置点击事件
        btnPopup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //弹出式菜单
                popupMenu.show();
            }
        });



    }

    private void showNotification() {
        //1.获取系统的通知服务
        NotificationManager manager =
                (NotificationManager) getApplicationContext().getSystemService(
                        Context.NOTIFICATION_SERVICE
                );
        //2.创建通知渠道，并关联给通知服务
        NotificationChannel channel = new NotificationChannel(
                "channelId",  //渠道id
                "channelName",   //渠道名称
                NotificationManager.IMPORTANCE_DEFAULT  //重要程度
        );
        manager.createNotificationChannel(channel);
        //3.创建通知构造器对象
        Notification.Builder builder = new Notification.Builder(
                this,//上下问环境
                "channelId"//关联的渠道id
        )
                .setContentTitle("健康提醒")
                .setContentText("今天吃早饭了吗?")
                .setSmallIcon(R.mipmap.ic_launcher);
        //4.由构造器对象来构造通知对象
        Notification notification = builder.build();
        //5.由通知服务发布通知
        manager.notify(0,notification);
        //取消通知
//        manager.cancel(0);

    }

    private void getViews() {
        btnNotify = findViewById(R.id.btn_notify);

        btnContextMenu = findViewById(R.id.btn_context_menu);

        btnPopup  = findViewById(R.id.btn_popup);
    }




    //加载菜单资源并关联给Activity
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //1.使用菜单加载器加载菜单资源
        //2.把菜单资源关联给默认选项菜单
        getMenuInflater().inflate(R.menu.file_menu,menu);

        return super.onCreateOptionsMenu(menu);
    }

    //给选项菜单注册item选择事件监听器
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //判断当前选择的是哪一个菜单项
        switch (item.getItemId()){
            case R.id.menu_file:
                Log.i("msg","文件菜单");
                break;
            case R.id.menu_file_open:
                Log.i("msg","文件-打开菜单");
                break;
            case R.id.menu_file_saves:
                Log.i("msg","文件-另存为菜单");
                break;
            case R.id.menu_edit:
                Log.i("msg","编辑菜单");
                break;
        }
        return super.onOptionsItemSelected(item);
    }



    //绑定上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        //获取menu资源，绑定给ACTivity的上下文菜单
        getMenuInflater().inflate(R.menu.file_menu,menu);

        super.onCreateContextMenu(menu, v, menuInfo);
    }

    //复写上下问菜单的回到方法


    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        //判断当前选择的是哪一个菜单项
        switch (item.getItemId()){
            case R.id.menu_file:
                Log.i("msg","文件菜单");
                break;
            case R.id.menu_file_open:
                Log.i("msg","文件-打开菜单");
                break;
            case R.id.menu_file_saves:
                Log.i("msg","文件-另存为菜单");
                break;
            case R.id.menu_edit:
                Log.i("msg","编辑菜单");
                break;
        }



        return super.onContextItemSelected(item);
    }
}









